package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.Method;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;
import com.honey.mybatis.generator.page.PageConstant;

/**
 * 按照selectByDynamic查询
 * @author Administrator
 *
 */
public class SelectByDynamicMethod extends AbstractCreateInterfaceMethod{

	private static final String METHOD_NAME="selectByDynamic";

	private boolean isUseSession = false ;

	private boolean isPage = false ;
		

	public SelectByDynamicMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	public SelectByDynamicMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn,
			boolean  isUseSession,
			boolean  isPage
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
		this.isUseSession = isUseSession;
		this.isPage = isPage;
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {	
		IfBlock ifBlock = null;
		if( isUseSession()){
			ifBlock = new IfBlock(new StringBuilder().append( DaoConstant.SQL_SESSION_NAME ).append(" == null").toString());
			ifBlock.addBodyLine("return null");
			method.addBlocks(ifBlock);
		}
		ifBlock = new IfBlock(new StringBuilder()
			.append(DaoConstant.CONDITION_VARIABLE_NAME)
			.append(" == null") 
			.append( " || "+ DaoConstant.CONDITION_VARIABLE_NAME +".getOredCriteria().size() == 0" )
			.toString()
		);
		ifBlock.addBodyLine(DaoConstant.CONDITION_VARIABLE_NAME+" = null");
		method.addBlocks(ifBlock);
		
		if(isUsePage()){
			addBody2(method);
		}else{
			addBody1(method);
		}
	}
	
	private void addBody1(Method method){
		method.addBodyLine(
				new StringBuilder()
				.append(method.getReturnType().getFullName())
				.append(" list = ")
				//.append("super.")
				.append(mybatisPlugin.getDependentModel().call(null))
				.append("selectList ( ")
				.append( isUseSession()? DaoConstant.SQL_SESSION_NAME+" , ":""  )
				//.append(DaoConstant.SELECT_BY_DYNAMIC_FIELD_NAME)
				.append(super.getMapperId().getSelectByDynamicMapperId())
				.append(" , ")
				//.append(DaoConstant.SELECT_COUNT_FIELD_NAME)
				.append(super.getMapperId().getSelectCountMapperId())
				.append(" , ")
				.append(DaoConstant.CONDITION_VARIABLE_NAME).append(", new "+PageConstant.PAGE_NAME+"(-1,-1,-1) )")
				.toString()
		);
		method.addBodyLine("return list ");
	}
	
	private void addBody2(Method method){
		IfBlock  ifBlock = new IfBlock(PageConstant.PAGE_VARIABLE_NAME+" != null && "+DaoConstant.CONDITION_VARIABLE_NAME +" != null");
		ifBlock.addBodyLine(DaoConstant.CONDITION_VARIABLE_NAME +".setOrderBy( "+PageConstant.PAGE_VARIABLE_NAME+".getOrderBy() ) ");
		method.addBlocks(ifBlock);
		
		method.addBodyLine(
				new StringBuilder()
				.append(method.getReturnType().getFullName())
				.append(" list = ")
				.append(mybatisPlugin.getDependentModel().call(null))
				.append("selectList ( ")
				.append( isUseSession()? DaoConstant.SQL_SESSION_NAME+" , ":""  )
				//.append(DaoConstant.SELECT_BY_DYNAMIC_FIELD_NAME)
				.append(super.getMapperId().getSelectByDynamicMapperId())
				.append(" , ")
				//.append(DaoConstant.SELECT_COUNT_FIELD_NAME)
				.append(super.getMapperId().getSelectCountMapperId())
				.append(" , ").append(DaoConstant.CONDITION_VARIABLE_NAME).append(", "+PageConstant.PAGE_VARIABLE_NAME+" )")
				.toString()
		);
		method.addBodyLine("return list ");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {		
		String answer = null;
		if( !isUseSession ){
			answer = METHOD_NAME;
		}else if( isUseSession  && mybatisPlugin.isEnableUseSqlSession() ){
			answer = METHOD_NAME;
		}
		return answer;
	}

	/**
	 * @return the isUseSession
	 */
	@Override
	public final boolean isUseSession() {
		return isUseSession;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		String[] answer = new String[]{
			"动态条件查询记录。"
		};
		return answer;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return new FullyQualifiedJavaType("java.util.List<"+ super.getEntityClassName(introspectSchema.getSchema()) +">");
	}

	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.dao.method.AbstractCreateInterfaceMethod#isUsePage()
	 */
	@Override
	public boolean isUsePage() {
		return this.isPage;
	}
	
}
